package com.ymatou.socialbizer.testcase;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.socialbizer.parameter.RecommendNotesBean;
import com.ymatou.socialbizer.service.RecommendNotesCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.database.mongo.NoteMongoWapper;
import com.ymttest.utils.YMTDateUtil;

public class Ts_RecommendNotes {
	private static RecommendNotesBean RecommendNoteBean;
	private static RecommendNotesCall RecommendNoteCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("大首页拔草感言接口");
	}

	@Before
	public void caseUp() {
		RecommendNoteBean=new RecommendNotesBean();
		RecommendNoteCall=new RecommendNotesCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(RecommendNoteCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_RecommendNotes_001() {
		Logger.start(true,"拔草感言-验证接口返回公共数据 ");
		try {
			//设置参数
			
			RecommendNoteBean.setInfo("userid:111,cookieid:111555555555,yid:abcde");
			RecommendNoteBean.setLimit("0,10");
			
			RecommendNoteCall.setData(RecommendNoteBean);
			RecommendNoteCall.callService();	
			
			JSONArray hits = RecommendNoteCall.getHits();
			
			Logger.verifyEquals("OK",RecommendNoteCall.getCommonInfo("status") , "检查接口状态值");
			Logger.verifyNotNull(RecommendNoteCall.getCommonInfo("took"), "took不为空");
			Logger.verifyEquals(false, RecommendNoteCall.getCommonInfoBoo("downgrade"), "验证downgrade返回false");
			Logger.verifyEquals(false, RecommendNoteCall.getCommonInfoBoo("timeout"), "验证timeout返回flase");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_RecommendNotes_002() {
		Logger.start(true,"拔草感言-验证接口返回数据是否有重复 ");
		try {
			//设置参数
			
			RecommendNoteBean.setInfo("userid:111,cookieid:111555555555,yid:abcde");
			//RecommendNoteBean.setLimit("0,10");
			
			RecommendNoteCall.setData(RecommendNoteBean);
			RecommendNoteCall.callService();	
			
			JSONArray hitsAry = RecommendNoteCall.getHits();
			
			List<String> list = new ArrayList<String>();

			for (int i = 1; i <= hitsAry.length(); i++) {
				
				String ids = hitsAry.get(i - 1).toString();
				JSONObject json = new JSONObject(ids);
				
				String id = json.getString("noteid");
				
				if (list.contains(id) == true) {

					Logger.verifyEquals(false, list.contains(id), "验证第" + i+ "条返回数据的noteid存在重复值" + id);

				}else{
					Logger.verifyEquals(false, list.contains(id), "验证第" + i+ "条返回数据的noteid不存在重复值" + id);
					list.add(id);
				}
			
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_RecommendNotes_003() {
		Logger.start(true,"拔草感言 - 检查返回数据总数与数据库中统计是否一样 ");
		try {
			// 设置参数
			RecommendNoteBean.setInfo("userid:111,cookieid:111555555555,yid:abcde");
			RecommendNoteBean.setLimit("0,10");
			RecommendNoteCall.setData(RecommendNoteBean);
			RecommendNoteCall.callService();
			
			//设置七天之内的时间
			String end = YMTDateUtil.getDateForMongoFormat(0, 0, 0);
			String start = YMTDateUtil.getDateForMongoFormat(-7, 0, 0);

			
			// 链接mongo库查询数据
			NoteMongoWapper notes = new NoteMongoWapper();
			// 通过mongo获取7天之内的拔草笔记
			ArrayList pendNote = notes.getRecommendNotes(start,end);
		
			Logger.verifyEquals(pendNote.size(), RecommendNoteCall.getTotal(), "检查返回数据总数与数据库中统计的结果是否一致");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_RecommendNotes_004() {
		Logger.start(true,"拔草感言 - 检查接口返回数据是否为小编推荐 ");
		try {
			// 设置参数
			RecommendNoteBean.setInfo("userid:111,cookieid:111555555555,yid:abcde");
			//RecommendNoteBean.setLimit("0,10");
			RecommendNoteCall.setData(RecommendNoteBean);
			RecommendNoteCall.callService();
			
			NoteMongoWapper notes = new NoteMongoWapper();
			
			for(int i =1;i<=RecommendNoteCall.getHits().length();i++){
				String ids = RecommendNoteCall.getHits().get(i - 1).toString();
				JSONObject json = new JSONObject(ids);
				
				int noteid = json.getInt("noteid");
				
				//连接mongo查询对应笔记是否为小编推荐
				Map noteInfo = notes.getNoteInfoByNoteId(noteid);
				
				Logger.verifyEquals(true,noteInfo.get("IsRecommend") , "检查noteid为："+noteid+"的 第"+i+"条笔记是否为推荐笔记");
				
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_RecommendNotes_005() {
		Logger.start(true,"拔草感言 - 检查接口返回数据是否为一周之内的新增笔记");
		try {
			// 设置参数
			RecommendNoteBean.setInfo("userid:111,cookieid:111555555555,yid:abcde");
			RecommendNoteCall.setData(RecommendNoteBean);
			RecommendNoteCall.callService();
			
			NoteMongoWapper notes = new NoteMongoWapper();
			
			//计算七天之内的开始和结束时间
			Date end = new Date();
			
			Calendar c = Calendar.getInstance();
			c.add(Calendar.DATE, -7);
			Date start = c.getTime();
		
			
			for(int i =1;i<=RecommendNoteCall.getHits().length();i++){
				String ids = RecommendNoteCall.getHits().get(i - 1).toString();
				JSONObject json = new JSONObject(ids);
				
				int noteid = json.getInt("noteid");
				
				//连接mongo查询对应笔记是否为小编推荐
				Map noteInfo = notes.getNoteInfoByNoteId(noteid);
				Date mongDate= (Date) noteInfo.get("AddTime");
				
				boolean timeRestul =false;
				if(   start.getTime() <= mongDate.getTime() && mongDate.getTime() <= end.getTime()){
					timeRestul = true;
				}
				
				Logger.verifyEquals(true, timeRestul, "检查noteid为："+noteid+"的 第"+i+"条笔记是否是一周内新增笔记");
				
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_RecommendNotes_006() {
		Logger.start(true,"拔草感言 - 检查接口返回数据是否为买家笔记 ");
		try {
			// 设置参数
			RecommendNoteBean.setInfo("userid:111,cookieid:111555555555,yid:abcde");
			RecommendNoteCall.setData(RecommendNoteBean);
			RecommendNoteCall.callService();
			
			NoteMongoWapper notes = new NoteMongoWapper();
			
			for(int i =1;i<=RecommendNoteCall.getHits().length();i++){
				String ids = RecommendNoteCall.getHits().get(i - 1).toString();
				JSONObject json = new JSONObject(ids);
				
				int noteid = json.getInt("noteid");
				
				//连接mongo查询对应笔记是否为小编推荐
				Map noteInfo = notes.getNoteInfoByNoteId(noteid);
				
				Logger.verifyEquals(0,noteInfo.get("NoteSource") , "检查noteid为："+noteid+"的 第"+i+"条笔记是否为买家笔记");
				
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_RecommendNotes_007() {
		Logger.start(true,"拔草感言 - 检查接口返回笔记数据是否有关联订单信息 ");
		try {
			// 设置参数
			RecommendNoteBean.setInfo("userid:111,cookieid:111555555555,yid:abcde");
			//RecommendNoteBean.setLimit("0,10");
			RecommendNoteCall.setData(RecommendNoteBean);
			RecommendNoteCall.callService();
			
			NoteMongoWapper notes = new NoteMongoWapper();
			
			for(int i =1;i<=RecommendNoteCall.getHits().length();i++){
				String ids = RecommendNoteCall.getHits().get(i - 1).toString();
				JSONObject json = new JSONObject(ids);
				
				int noteid = json.getInt("noteid");
				
				//连接mongo查询对应笔记是否为小编推荐
				Map noteInfo = notes.getNoteInfoByNoteId(noteid);
				
				//获取订单号
				Map NewNoteInfoObject =  (Map) noteInfo.get("NewNoteInfo");
				int orderid = (Integer) NewNoteInfoObject.get("OrderId");
				
				Logger.verifyEquals(true,orderid>0 , "检查noteid为："+noteid+"的 第"+i+"条笔记是否有关联订单号");
				
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_RecommendNotes_008() {
		Logger.start(true,"拔草感言 - 当能匹配到用户信息时，检查接口返回数据是否有按用户兴趣度排序 ");
		try {
			// 设置参数
			RecommendNoteBean.setInfo("userid:111,cookieid:111555555555,yid:abcde");
			//RecommendNoteBean.setLimit("0,10");
			RecommendNoteCall.setData(RecommendNoteBean);
			RecommendNoteCall.callService();
			
			
			for (int i = 1; i <=RecommendNoteCall.getHits().length(); i++) {
				for (int j = i+1; j <= RecommendNoteCall.getHits().length(); j++) {
					
					String noteInfo1 = RecommendNoteCall.getHits().get(i - 1).toString();
					JSONObject noteInfoJson1 = new JSONObject(noteInfo1);
					Double popularity1 =  (Double) noteInfoJson1.get("popularity");
					
					String noteInfo2 = RecommendNoteCall.getHits().get(j - 1).toString();
					JSONObject noteInfoJson2 = new JSONObject(noteInfo2);
					Double popularity2= (double) noteInfoJson1.getInt("popularity");
					
					Logger.verifyEquals(true, popularity1>popularity2, "检查排在第"+i+"位的标签热度值为"+popularity1+"的笔记比排在第"+j+"位的标签热度值为:"+popularity2+"笔记热度值高");
				}
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	//TODO 拔草感言 - 当没有匹配到用户信息时，检查接口返回数据是否有按标签热度排序
	
}
